package com.wltt.product.frfront.executors;

import com.wltt.product.frfront.config.exception.BusinessException;
import com.wltt.product.frfront.config.exception.RobotException;
import com.wltt.product.frfront.config.javafx.utils.AlertUtil;
import com.wltt.product.frfront.config.robot.WlttRobot;
import com.wltt.product.frfront.constants.RobotJoinConstant;
import com.wltt.product.frfront.constants.RobotMoveConstant;
import com.wltt.product.frfront.holder.RobotHolder;
import com.wltt.product.frfront.model.vo.JointMoveREQ;
import javafx.scene.control.Alert;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpStatus;

public class JointMoveForJavaFxExecutor implements Runnable {

    private static final Logger logger = LogManager.getLogger(JointMoveForJavaFxExecutor.class);

    /**
     * 1-关节1(或x轴)，2-关节2(或y轴)，3-关节3(或z轴)，4-关节4(或绕x轴旋转)，5-关节5(或绕y轴旋转)，6-关节6(或绕z轴旋转)
     */
    private int nb;

    /**
     * 0-负方向，1-正方向
     */
    private int dir;

    public JointMoveForJavaFxExecutor(int nb, int dir) {
        this.nb = nb;
        this.dir = dir;
    }

    @Override
    public void run() {
        if (!RobotHolder.setBusy()) {
            return;
        }
        if (!RobotHolder.setRobotMoving()) {
            return;
        }

        try {
            RobotHolder.getRobot().Mode(0);
            RobotHolder.getRobot().StartJOG(0, nb, dir, 5, 50, RobotHolder.getRobot().getRobotPO().getMaxStep());
            RobotHolder.getRobot().Mode(1);
        } catch (RobotException e) {
            AlertUtil.alert(Alert.AlertType.ERROR, "错误", "jog点动(关节)指令发送失败", e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            RobotHolder.setNoBusy();
            RobotHolder.setRobotNoMoving();
        }
    }
}
